package com.sgb.contract.view.ui.list;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.sgb.contract.BR;
import com.sgb.contract.R;
import com.sgb.contract.model.ContractListSearchBean;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.xrecyclerview.XRecyclerView;

import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.BindingAdapter;

/**
 * 合同列表adapter
 * <p>合同的六种状态对应
 * 合同状态         发起方                       参与方
 * 拟定中（初次）    退回合同、发送合同、查看合同     无该条数据
 * 拟定中（修改）    退回合同、发送合同、查看合同     无操作按钮
 * 待对方签署       查看合同                     查看合同
 * 待我方签署       退回合同、签署合同             修改合同、签署合同
 * 已完成          查看合同                     查看合同
 * 已过期          查看合同                     查看合同
 * 已作废          查看合同                     查看合同
 * </p>
 *
 * @author cyj
 */
public class ContractListAdapter extends BaseRecycleAdapter<ContractListSearchBean.ContractListInfo> {

    @SuppressLint("StaticFieldLeak")
    public static XRecyclerView recyclerView;
    private OnClickListener listener;
    public final String[] TAG = {"InvalidContract", "SendContract", "SelectContract", "SignContract", "RejectedContract"};

    public ContractListAdapter(Context context) {
        super(context, R.layout.contract_adapter_item_contract_list, null);
    }

    @BindingAdapter("bindAdapter")
    public static void recycleViewBindAdapter(XRecyclerView xRecyclerView, XRecyclerView.Adapter<ItemViewHolder> adapter) {
        xRecyclerView.setAdapter(adapter);
        recyclerView = xRecyclerView;
    }

    @Override
    public void convert(ItemViewHolder holder, ContractListSearchBean.ContractListInfo data) {
        holder.setBinding(BR.data, data);
        holder.setBinding(BR.adapter, this);

      /*  holder.getView(R.id.tv_show).setVisibility((data.status == 4 || data.status == 5 || data.status == 2) ? View.GONE : data.isTaker ? View.GONE :
                data.isShow()?View.VISIBLE:View.GONE);*/
    }

    /**
     * 代理合同状态点击事件到adapter
     */
    public void delegateClick(View view, ContractListSearchBean.ContractListInfo data) {
        if (null != this.listener) {
            this.listener.onClick(view, data);
        }
    }

    /**
     * status 合同状态 1:拟定中，2：签署中，3：已完成，4：已过期，5，已作废
     *
     * @param bean ContractListSearchBean.ContractListInfo
     * @return Drawable
     */
    public Drawable setContractStateImg(ContractListSearchBean.ContractListInfo bean) {
        if (1 == bean.status) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state1, null);
        } else if (2 == bean.status && 6 == bean.appSignTag) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state2, null);
        } else if (2 == bean.status && 7 == bean.appSignTag) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state3, null);
        } else if (3 == bean.status) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state4, null);
        } else if (4 == bean.status) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state5, null);
        } else if (5 == bean.status) {
            return ResourcesCompat.getDrawable(context.getResources(), R.mipmap.icon_sign_state6, null);
        }
        return null;
    }

    public int setContractApprovalStatusColor(ContractListSearchBean.ContractListInfo bean) {
        if (bean.appAuditStatus == 0) {
            return ResourcesCompat.getColor(context.getResources(), R.color.color_ff333333, null);
        } else if (1 == bean.appAuditStatus) {
            return ResourcesCompat.getColor(context.getResources(), R.color.color_5792FD, null);
        } else if (2 == bean.appAuditStatus) {
            return ResourcesCompat.getColor(context.getResources(), R.color.contract_color_06C764, null);
        } else if (3 == bean.appAuditStatus) {
            return ResourcesCompat.getColor(context.getResources(), R.color.color_EF4033, null);
        }
        return ResourcesCompat.getColor(context.getResources(), R.color.color_ff333333, null);
    }

    public String setContractApprovalStatusValue(ContractListSearchBean.ContractListInfo bean) {
        if (bean.appAuditStatus == 0) {
            return "/";
        } else if (1 == bean.appAuditStatus) {
            return "审批中";
        } else if (2 == bean.appAuditStatus) {
            return "已通过";
        } else if (3 == bean.appAuditStatus) {
            return "已拒绝";
        }
        return "/";
    }

    public void setOnclickListener(OnClickListener listener) {
        this.listener = listener;
    }

    public void refreshComplete() {
        recyclerView.refreshComplete();
    }

    public void loadMoreComplete() {
        recyclerView.loadMoreComplete();
    }

    public void setNoMore(boolean isNoMore) {
        recyclerView.setNoMore(isNoMore);
    }

    public interface OnClickListener {
        void onClick(View v, ContractListSearchBean.ContractListInfo bean);
    }
}
